/**
 * 
 */
package com.joinfun.rdsdk.mingdao.api;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.joinfun.rdsdk.mingdao.api.base.BaseRequest;
import com.joinfun.rdsdk.mingdao.api.base.BaseUrl;
import com.joinfun.rdsdk.mingdao.entity.json.Auth;
import com.joinfun.rdsdk.mingdao.utils.ConfigurationManager;
import com.joinfun.rdsdk.mingdao.utils.JsonConverter;

/**
 * @author FMK
 * 2014年1月5日下午12:04:33
 * grant_type = authorization_code
 */
public class Oauth2API {

	/**
	 * web登陆明道  通过code获取令牌
	 * @author FMK
	 * 2014年1月5日下午1:13:02
	 * @param code
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public static Auth accessToken(String code) throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("app_key", ConfigurationManager.get("app_key"));
		params.put("app_secret", ConfigurationManager.get("app_secret"));
		params.put("redirect_uri", ConfigurationManager.get("redirect_uri"));
		params.put("grant_type", "authorization_code");
		params.put("format", "json");
		params.put("code", code);
		return (Auth) JsonConverter.convertToJOPO(BaseRequest.request(BaseUrl.OAUTH2_ACCESS_TOKEN, "GET", params), Auth.class);
	}
	
	@Deprecated
	public static Auth accessTokenByUsernameAndPassword(String username, String password) throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("app_key", ConfigurationManager.get("app_key"));
		params.put("app_secret", ConfigurationManager.get("app_secret"));
		params.put("redirect_uri", ConfigurationManager.get("redirect_uri"));
		params.put("grant_type", "password");
		params.put("username", username);
		params.put("password", password);
		params.put("format", "json");
		return (Auth) JsonConverter.convertToJOPO(BaseRequest.request(BaseUrl.OAUTH2_ACCESS_TOKEN, "GET", params), Auth.class);
	}
	
	/**
	 * 通过刷新令牌  重新获取新的令牌
	 * @author FMK
	 * 2014年1月5日下午1:08:01
	 * @param refresh_token
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public static Auth accessTokenByRefreshToken(String refresh_token) throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("app_key", ConfigurationManager.get("app_key"));
		params.put("app_secret", ConfigurationManager.get("app_secret"));
		params.put("redirect_uri", ConfigurationManager.get("redirect_uri"));
		params.put("grant_type", "refresh_token");
		params.put("refresh_token", refresh_token);
		params.put("format", "json");
		return (Auth) JsonConverter.convertToJOPO(BaseRequest.request(BaseUrl.OAUTH2_ACCESS_TOKEN, "GET", params), Auth.class);
	}
}
